﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using System.IO;
using _6.Util;

namespace _6
{
    public partial class Form1 : Form
    {
        private Util.SQLite sqlite = new Util.SQLite();
        public Form1()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            sqlite.InitializeDatabase();
            comboBox1.Items.Add("Student");
            comboBox1.Items.Add("Class");
            comboBox1.Items.Add("School");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Student")
            {
                richTextBox1.Text += "切换到学生\n";
                dataGridView1.DataSource =sqlite.Adapter(sqlite.SelectFromDatabase(new Model.StudentInfo()));
            }else if (comboBox1.Text == "Class")
            {
                richTextBox1.Text += "切换到班级\n";
                dataGridView1.DataSource = sqlite.Adapter(sqlite.SelectFromDatabase(new Model.ClassInfo()));
            }else if (comboBox1.Text == "School")
            {
                richTextBox1.Text += "切换到学校\n";
                dataGridView1.DataSource = sqlite.Adapter(sqlite.SelectFromDatabase(new Model.SchoolInfo()));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var test = dataGridView1.SelectedRows;
            if (comboBox1.Text == "Student")
            {
                Model.StudentInfo student = new Model.StudentInfo();
                student.StudentId = Convert.ToInt32(test[0].Cells[0].Value);
                student.Sex = test[0].Cells[1].Value.ToString();
                student.SchoolId = Convert.ToInt32(test[0].Cells[2].Value);
                student.ClassId = Convert.ToInt32(test[0].Cells[3].Value);
                student.Name = test[0].Cells[4].Value.ToString();
                sqlite.ExecuteQuery(sqlite.InsertToDatabase(student));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.StudentInfo()));
            }else if (comboBox1.Text == "Class")
            {
                Model.ClassInfo classInfo = new Model.ClassInfo();
                classInfo.ClassId = Convert.ToInt32(test[0].Cells[0].Value);
                classInfo.Name = test[0].Cells[1].Value.ToString();
                classInfo.SchoolId = Convert.ToInt32(test[0].Cells[2].Value);
                classInfo.Count = Convert.ToInt32(test[0].Cells[3].Value);
                sqlite.ExecuteQuery(sqlite.InsertToDatabase(classInfo));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.ClassInfo()));
            }else if (comboBox1.Text == "School")
            {
                Model.SchoolInfo schoolInfo = new Model.SchoolInfo();
                schoolInfo.SchoolId = Convert.ToInt32(test[0].Cells[0].Value);
                schoolInfo.Name = test[0].Cells[1].Value.ToString();
                sqlite.ExecuteQuery(sqlite.InsertToDatabase(schoolInfo));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.SchoolInfo()));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            throw new System.NotImplementedException();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var test= dataGridView1.SelectedRows;
            if (comboBox1.Text == "Student")
            {
                Model.StudentInfo student = new Model.StudentInfo();
                student.StudentId = Convert.ToInt32(test[0].Cells[0].Value);
                sqlite.ExecuteQuery(sqlite.DeleteFromDatabase(student));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.StudentInfo()));
            }else if (comboBox1.Text == "Class")
            {
                Model.ClassInfo classInfo = new Model.ClassInfo();
                classInfo.ClassId = Convert.ToInt32(test[0].Cells[0].Value);
                sqlite.ExecuteQuery(sqlite.DeleteFromDatabase(classInfo));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.ClassInfo()));
            }else if (comboBox1.Text == "School")
            {
                Model.SchoolInfo schoolInfo = new Model.SchoolInfo();
                schoolInfo.SchoolId = Convert.ToInt32(test[0].Cells[0].Value);
                sqlite.ExecuteQuery(sqlite.DeleteFromDatabase(schoolInfo));
                sqlite.Adapter(sqlite.SelectFromDatabase(new Model.SchoolInfo()));
            }
        }
    }
}